home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / png / zlib09 / example.c < prev    next >
C/C++ Source or Header  |  1995-05-01  |  7KB  |  289 lines

  1. /* example.c -- usage example of the zlib compression library
  2.  * Copyright (C) 1995 Jean-loup Gailly.
  3.  * For conditions of distribution and use, see copyright notice in zlib.h 
  4.  */
  5.  
  6. /* $Id: example.c,v 1.7 1995/05/01 16:57:22 jloup Exp $ */
  7.  
  8. #include <stdio.h>
  9. #include "zlib.h"
  10.  
  11. #ifdef STDC
  12. #  include <string.h>
  13. #endif
  14.  
  15. extern void exit  __P((int));
  16.  
  17. #define BUFLEN 4096
  18.  
  19. #define local static
  20. /* For MSDOS and other systems with limitation on stack size. For Unix,
  21.     #define local
  22.    works also.
  23.  */
  24.  
  25. #define CHECK_ERR(err, msg) { \
  26.     if (err != Z_OK) { \
  27.         fprintf(stderr, "%s error: %d\n", msg, err); \
  28.     exit(1); \
  29.     } \
  30. }
  31.  
  32. char *hello = "hello world";
  33.  
  34. void test_compress __P((void));
  35. void test_gzio     __P((char *out, char *in));
  36. void test_deflate  __P((Byte compr[]));
  37. void test_inflate  __P((Byte compr[]));
  38. void main          __P((int argc, char *argv[]));
  39.  
  40. /* ===========================================================================
  41.  * Test compress() and uncompress()
  42.  */
  43. void test_compress()
  44. {
  45.     local Byte compr[BUFLEN];
  46.     uLong comprLen = sizeof(compr);
  47.     local Byte uncompr[BUFLEN];
  48.     uLong uncomprLen = sizeof(uncompr);
  49.     int err;
  50.     uLong len = strlen(hello)+1;
  51.  
  52.     err = compress(compr, &comprLen, (Byte*)hello, len);
  53.     CHECK_ERR(err, "compress");
  54.  
  55.     strcpy((char*)uncompr, "garbage");
  56.  
  57.     err = uncompress(uncompr, &uncomprLen, compr, comprLen);
  58.     CHECK_ERR(err, "uncompress");
  59.  
  60.     if (strcmp((char*)uncompr, hello)) {
  61.     fprintf(stderr, "bad uncompress\n");
  62.     } else {
  63.     printf("uncompress(): %s\n", uncompr);
  64.     }
  65. }
  66.  
  67. /* ===========================================================================
  68.  * Test read/write of .gz files
  69.  */
  70. void test_gzio(out, in)
  71.     char *out; /* output file */
  72.     char *in;  /* input file */
  73. {
  74.     local Byte uncompr[BUFLEN];
  75.     int uncomprLen = sizeof(uncompr);
  76.     int err;
  77.     int len = strlen(hello)+1;
  78.     gzFile file;
  79.  
  80.     file = gzopen(out, "wb");
  81.     if (file == NULL) {
  82.     fprintf(stderr, "gzopen error\n");
  83.     exit(1);
  84.     }
  85.  
  86.     if (gzwrite(file, hello, len) != len) {
  87.     fprintf(stderr, "gzwrite err: %s\n", gzerror(file, &err));
  88.     }
  89.     gzclose(file);
  90.  
  91.     file = gzopen(in, "rb");
  92.     if (file == NULL) {
  93.     fprintf(stderr, "gzopen error\n");
  94.     }
  95.     strcpy((char*)uncompr, "garbage");
  96.  
  97.     uncomprLen = gzread(file, uncompr, uncomprLen);
  98.     if (uncomprLen != len) {
  99.     fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
  100.     }
  101.     gzclose(file);
  102.  
  103.     if (strcmp((char*)uncompr, hello)) {
  104.     fprintf(stderr, "bad gzread\n");
  105.     } else {
  106.     printf("gzread(): %s\n", uncompr);
  107.     }
  108. }
  109.  
  110. /* ===========================================================================
  111.  * Test deflate() with small buffers
  112.  */
  113. void test_deflate(compr)
  114.     Byte compr[];
  115. {
  116.     z_stream c_stream; /* compression stream */
  117.     int err;
  118.     int len = strlen(hello)+1;
  119.  
  120.     c_stream.zalloc = (alloc_func)0;
  121.     c_stream.zfree = (free_func)0;
  122.  
  123.     err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
  124.     CHECK_ERR(err, "deflateInit");
  125.  
  126.     c_stream.next_in  = (Byte*)hello;
  127.     c_stream.next_out = compr;
  128.  
  129.     while (c_stream.total_in != (uLong)len) {
  130.     c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
  131.     err = deflate(&c_stream, Z_NO_FLUSH);
  132.     CHECK_ERR(err, "deflate");
  133.     }
  134.     /* Finish the stream, still forcing small buffers: */
  135.     for (;;) {
  136.     c_stream.avail_out = 1;
  137.     err = deflate(&c_stream, Z_FINISH);
  138.     if (err == Z_STREAM_END) break;
  139.     CHECK_ERR(err, "deflate");
  140.     }
  141.  
  142.     err = deflateEnd(&c_stream);
  143.     CHECK_ERR(err, "deflateEnd");
  144. }
  145.  
  146. /* ===========================================================================
  147.  * Test inflate() with small buffers
  148.  */
  149. void test_inflate(compr)
  150.     Byte compr[];
  151. {
  152.     local Byte uncompr[BUFLEN];
  153.     int err;
  154.     z_stream d_stream; /* decompression stream */
  155.  
  156.     strcpy((char*)uncompr, "garbage");
  157.  
  158.     d_stream.zalloc = (alloc_func)0;
  159.     d_stream.zfree = (free_func)0;
  160.  
  161.     err = inflateInit(&d_stream);
  162.     CHECK_ERR(err, "inflateInit");
  163.  
  164.     d_stream.next_in  = compr;
  165.     d_stream.next_out = uncompr;
  166.  
  167.     for (;;) {
  168.     d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
  169.     err = inflate(&d_stream, Z_NO_FLUSH);
  170.     if (err == Z_STREAM_END) break;
  171.     CHECK_ERR(err, "inflate");
  172.     }
  173.  
  174.     err = inflateEnd(&d_stream);
  175.     CHECK_ERR(err, "inflateEnd");
  176.  
  177.     if (strcmp((char*)uncompr, hello)) {
  178.     fprintf(stderr, "bad inflate\n");
  179.     } else {
  180.     printf("inflate(): %s\n", uncompr);
  181.     }
  182. }
  183.  
  184. /* ===========================================================================
  185.  * Test deflate() with full flush
  186.  */
  187. void test_flush(compr)
  188.     Byte compr[];
  189. {
  190.     z_stream c_stream; /* compression stream */
  191.     int err;
  192.     int len = strlen(hello)+1;
  193.  
  194.     c_stream.zalloc = (alloc_func)0;
  195.     c_stream.zfree = (free_func)0;
  196.  
  197.     err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
  198.     CHECK_ERR(err, "deflateInit");
  199.  
  200.     c_stream.next_in  = (Byte*)hello;
  201.     c_stream.next_out = compr;
  202.     c_stream.avail_in = 3;
  203.     c_stream.avail_out = BUFLEN;
  204.     err = deflate(&c_stream, Z_FULL_FLUSH);
  205.     CHECK_ERR(err, "deflate");
  206.  
  207.     compr[3]++; /* force an error in first compressed block */
  208.     c_stream.avail_in = len - 3;
  209.  
  210.     err = deflate(&c_stream, Z_FINISH);
  211.     if (err != Z_STREAM_END) {
  212.     CHECK_ERR(err, "deflate");
  213.     }
  214.     err = deflateEnd(&c_stream);
  215.     CHECK_ERR(err, "deflateEnd");
  216. }
  217.  
  218. /* ===========================================================================
  219.  * Test inflateSync()
  220.  */
  221. void test_sync(compr)
  222.     Byte compr[];
  223. {
  224.     local Byte uncompr[BUFLEN];
  225.     int err;
  226.     z_stream d_stream; /* decompression stream */
  227.  
  228.     strcpy((char*)uncompr, "garbage");
  229.  
  230.     d_stream.zalloc = (alloc_func)0;
  231.     d_stream.zfree = (free_func)0;
  232.  
  233.     err = inflateInit(&d_stream);
  234.     CHECK_ERR(err, "inflateInit");
  235.  
  236.     d_stream.next_in  = compr;
  237.     d_stream.next_out = uncompr;
  238.     d_stream.avail_in = 2; /* just read the zlib header */
  239.     d_stream.avail_out = sizeof(uncompr);
  240.  
  241.     inflate(&d_stream, Z_NO_FLUSH);
  242.     CHECK_ERR(err, "inflate");
  243.  
  244.     d_stream.avail_in = BUFLEN-2; /* let inflate read all compressed data */
  245.     err = inflateSync(&d_stream); /* skip the damaged part */
  246.     CHECK_ERR(err, "inflateSync");
  247.  
  248.     err = inflate(&d_stream, Z_FINISH);
  249.     if (err != Z_DATA_ERROR) {
  250.         fprintf(stderr, "inflate should report DATA_ERROR\n");
  251.     /* Because of incorrect adler32 */
  252.     }
  253.     err = inflateEnd(&d_stream);
  254.     CHECK_ERR(err, "inflateEnd");
  255.  
  256.     printf("after inflateSync(): %s\n", uncompr);
  257. }
  258.  
  259. /* ===========================================================================
  260.  * Usage:  example [output.gz  [input.gz]]
  261.  */
  262.  
  263. void main(argc, argv)
  264.     int argc;
  265.     char *argv[];
  266. {
  267.     local Byte compr[BUFLEN];
  268.  
  269.     if (zlib_version[0] != ZLIB_VERSION[0]) {
  270.     fprintf(stderr, "incompatible zlib version\n");
  271.     exit(1);
  272.  
  273.     } else if (strcmp(zlib_version, ZLIB_VERSION) != 0) {
  274.     fprintf(stderr, "warning: different zlib version\n");
  275.     }
  276.     test_compress();
  277.  
  278.     test_gzio((argc > 1 ? argv[1] : "foo.gz"),
  279.           (argc > 2 ? argv[2] : "foo.gz"));
  280.  
  281.     test_deflate(compr);
  282.     test_inflate(compr);
  283.  
  284.     test_flush(compr);
  285.     test_sync(compr);
  286.  
  287.     exit(0);
  288. }
  289.